<template>
  <div class="app-container">
    <!-- 条件搜索 -->
    <div class="filter-container">
      <!--按需调整-->
      <el-form ref="queryForm" :model="listQuery.param" size="small" :inline="true" label-width="auto">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="listQuery.param.userName" placeholder="用户名" class="filter-item" @keyup.enter.native="handleFilterQuery" />
        </el-form-item>
        <el-form-item label="发生时间" prop="happenTime">
          <el-date-picker v-model="listQuery.param.happenTime" type="date" format="yyyy-MM-dd" placeholder="请选择" @change="handleFilterQuery" />
        </el-form-item>
        <el-popover
          v-model="advanced"
          title="高级查询"
          placement="bottom-start"
          width="1000"
          trigger="click"
        >
          <el-form ref="advancedForm" :model="listQuery.param" size="small" :inline="true" label-width="100px" style="margin-left: 20px;">
            <el-form-item label="用户Id" prop="userId">
              <el-input v-model="listQuery.param.userId" placeholder="用户Id" class="filter-item" @keyup.enter.native="handleFilterQuery" />
            </el-form-item>
            <el-form-item label="用户名" prop="userName">
              <el-input v-model="listQuery.param.userName" placeholder="用户名" class="filter-item" @keyup.enter.native="handleFilterQuery" />
            </el-form-item>
            <el-form-item label="姓名" prop="name">
              <el-input v-model="listQuery.param.name" placeholder="姓名" class="filter-item" @keyup.enter.native="handleFilterQuery" />
            </el-form-item>
            <el-form-item label="发生时间" prop="happenTime">
              <el-input v-model="listQuery.param.happenTime" placeholder="发生时间" class="filter-item" @keyup.enter.native="handleFilterQuery" />
            </el-form-item>
            <el-form-item label="访问IP" prop="happenIp">
              <el-input v-model="listQuery.param.happenIp" placeholder="访问IP" class="filter-item" @keyup.enter.native="handleFilterQuery" />
            </el-form-item>
          </el-form>
          <template #reference>
            <el-button class="filter-item" type="primary" icon="el-icon-d-caret">高级</el-button>
          </template>
        </el-popover>
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 10px;" @click="handleFilterQuery">
          搜索
        </el-button>
        <el-button v-waves class="filter-item" icon="el-icon-refresh-right" @click="handleRestQuery">
          重置
        </el-button>
      </el-form>
    </div>
    <div class="filter-container">
      <div style="float: right;">
        <el-button icon="el-icon-refresh" circle @click="handleFilterQuery" />
      </div>
    </div>

    <!-- 数据表格 -->
    <el-table
      :key="tableKey"
      v-loading="loading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      scrollbar-always-on="true"
    >
      <el-table-column type="selection" width="50px" align="center" />
      <el-table-column label="序号" type="index" width="50px" align="center" />

      <!--按需调整-->
      <el-table-column label="用户Id" min-width="100px" align="left">
        <template v-slot="{row}">
          <span>{{ row.userId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名" min-width="100px" align="left">
        <template v-slot="{row}">
          <span>{{ row.userName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" min-width="100px" align="left">
        <template v-slot="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发生类型" min-width="100px" align="left">
        <template v-slot="{row}">
          <span>{{ row.happenType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发生时间" min-width="100px" align="left">
        <template v-slot="{row}">
          <span>{{ row.happenTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="访问IP" min-width="100px" align="left">
        <template v-slot="{row}">
          <span>{{ row.happenIp }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" min-width="100px" align="left">
        <template v-slot="{row}">
          <span>{{ row.remark }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租户" min-width="100px" align="left">
        <template v-slot="{row}">
          <span>{{ row.tenantId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" min-width="70px" align="left">
        <template v-slot="{row}">
          <span>{{ row.updatedBy }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="160px" align="left">
        <template v-slot="{row}">
          <span>{{ row.updatedTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 表格分页 -->
    <pagination :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="handleInit" />

  </div>
</template>

<script>
import { handlePageList, handleSave, handleDelete } from './api'
import permission from '@/directive/permission' // 权限判断指令
import waves from '@/directive/waves' // waves directive
// import checkPermission from '@/utils/permission' // 权限判断函数
// import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination/index.vue'

export default {
  // 当前页面名称（组件）
  name: 'SysLoginLogForm',
  // 引用组件
  components: { Pagination },
  directives: { permission, waves },
  filters: {},
  data() {
    return {
      // 列表table的key
      tableKey: 0,
      // 遮罩层
      loading: true,
      // table数据对象
      list: [],
      // 总条数
      total: 0,
      // table条件查询对象
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        // sort: '+id',
        param: {
          // 按需调整
          userId: undefined,
          userName: undefined,
          name: undefined,
          happenType: undefined,
          happenTime: undefined,
          happenIp: undefined
        }
      },
      // 高级查询
      advanced: false,
      // 状态字典
      // 表单数据对象
      formData: {},
      // 是否显示弹出层
      visible: false,
      // 弹出层状态值（新增和修改共用同一个表单页面）
      state: '',
      // 弹出层标题
      title: {
        update: '修改',
        create: '新增',
        detail: '详情'
      },
      // 权限标识
      permission: {},
      // 表单遮罩
      submitting: false,
      // 表单校验规则
      rules: {}
    }
  },
  computed: {},
  watch: {},
  created() {
    this.handleInit()
    this.handleRestFormData()
  },
  mounted() {},
  methods: {
    // 查询table数据对象
    handleInit() {
      this.loading = true
      handlePageList(this.listQuery).then(res => {
        this.list = res.data
        if (res.total) {
          this.total = Number(res.total)
        }
        this.loading = false
      })
    },
    // 条件查询table数据
    handleFilterQuery() {
      this.listQuery.pageNum = 1
      this.handleInit()
    },
    // 重置table查询条件
    handleRestQuery() {
      this.$refs.queryForm.resetFields()
      this.$refs.advancedForm.resetFields()
    },
    // 重置表单
    handleRestFormData() {
      // 按需调整
      this.formData = {
        id: undefined,
        userId: undefined,
        userName: undefined,
        name: undefined,
        happenType: undefined,
        happenTime: undefined,
        happenIp: undefined,
        createdBy: undefined,
        createdTime: undefined,
        updatedBy: undefined,
        updatedTime: undefined,
        tenantId: undefined,
        revision: undefined,
        deleted: undefined,
        status: 'Y',
        remark: ''
      }
    },
    // 更多点击操作项
    handleClick(item, row, index) {
      // 编辑
      if (item === 'edit') {
        this.handleUpdate(row, index)
      }
      // 删除
      if (item === 'del') {
        this.handleDelete(row, index)
      }
    },
    // 新增
    handleCreate() {
      this.state = 'create'
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 修改
    handleUpdate(row) {
      this.state = 'update'
      this.visible = true

      // 创建一个新的对象赋值
      this.formData = Object.assign({}, row)

      // 按需补充

      // 清理掉多余的属性值，避免数据不对问题
      delete this.formData.createdBy
      delete this.formData.createdTime
      delete this.formData.updatedBy
      delete this.formData.updatedTime
      delete this.formData.tenantId
      delete this.formData.revision
      delete this.formData.deleted

      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 详情
    handleDetail(row) {
      this.state = 'detail'
      this.visible = true
      // 创建一个新的对象赋值
      this.formData = Object.assign({}, row)
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 删除
    handleDelete(row, index) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 显示加载遮罩
        const loading = this.$loading({ lock: true, text: '处理中...', body: true, fullscreen: false, background: 'rgba(0, 0, 0, 0.05)' })
        handleDelete({ id: row.id }).then((res) => {
          loading.close()
          //  刷新table
          this.handleFilterQuery()
          this.$message({
            showClose: true,
            message: res.msg,
            type: 'success',
            duration: 4000
          })
        }).catch(() => {
          loading.close()
        })
      }).catch((err) => {
        console.error('异常-->:', err)
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 表单提交
    handleSubmitFormData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.submitting = true
          handleSave(this.formData).then((res) => {
            this.submitting = false
            //  刷新table
            this.handleFilterQuery()
            this.handleClose()
            this.$message({
              showClose: true,
              message: res.msg,
              type: 'success',
              duration: 4000
            })
          })
        }
      })
    },
    // 关闭弹出层
    handleClose() {
      this.handleRestFormData()
      this.visible = false
    }
  }
}
</script>
<style>
  .vue-treeselect__control{
    height: 32px;
  }
  .el-input.is-disabled .el-input__inner {
    background-color: #ffffff !important;
    cursor: pointer !important;
  }
  .el-radio.is-disabled .el-radio__inner {
    background-color: #ffffff !important;
    cursor: pointer !important;
  }
  .el-radio__input.is-disabled {
    background-color: #ffffff !important;
    cursor: pointer !important;
  }
  .el-radio__input.is-disabled.is-checked {
    background-color: #ffffff !important;
    cursor: pointer !important;
  }
  .el-radio__label {
    background-color: #ffffff !important;
    cursor: pointer !important;
  }
  .el-textarea.is-disabled .el-textarea__inner {
    background-color: #ffffff !important;
  }
  .vue-treeselect--disabled .vue-treeselect__control {
    background-color: #ffffff !important;
  }
  /* 自定义 ElPopover 标题下方分隔线 */
  .el-popover__title {
    position: relative;
    padding-bottom: 10px;
  }

  .el-popover__title::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 1px;
    background-color: #ebeef5;
  }
</style>
